﻿#include "searchbox.h"
#include "ui_searchbox.h"

Searchbox::Searchbox(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Searchbox)
{
    ui->setupUi(this);

    prefix=BAIDU_PREFIX;
    awake();
}

Searchbox::~Searchbox()
{
    delete ui;
}

void Searchbox::awake()
{
    QStringList items;
    QSqlQuery query;
    query.exec(QString("select label,detail from %1 where category='%2'")
               .arg(TD_PREFIX)
               .arg(PRE_SEARCH));

    while (query.next()) {
        QString label=query.value("label").toString();
        QString detail=query.value("detail").toString();
        items.append(label);
        traitMap.insert(label,detail);
    }

    if(!items.isEmpty())
    {
        ui->traits->addItems(items);
        ui->traits->setCurrentIndex(-1);
    }
}

void Searchbox::on_keyword_returnPressed()
{
    QString kw=ui->keyword->text().trimmed();
    if(kw.isEmpty())
        return;

    QString label=ui->traits->currentText();
    if(!label.isEmpty())
        prefix=traitMap.value(label);
    else
        prefix=BAIDU_PREFIX;

    QString url=prefix+kw;
    DesktopServices::openUrl(QUrl(url));
}
